﻿using System;
using System.Collections.Generic;
using System.Text;
using SwinGame;

namespace Survivor.src
{
    class TerrainManager : IManager<Tile>
    {
        const int MAX_TILES = 50;               //load from constant manager//
        const int TERRAIN_TILE_SIZE = 32;
        Camera                  _camera;
        Pathfinder              _pathfinder;
        List<SwinGame.Sprite>   _sprites;
        Tile[,]                 _tiles;
        bool                    _running = true;
        TerrainManager(Camera cam, Pathfinder pf)
        {
            _camera = cam;
            _pathfinder = pf;
        }
        public void AddChild(Tile child) {}
        public bool RemoveChild(Tile child) { return false; }

        public void Draw(double dt)
        {
            for (int j = 0; j < MAX_TILES; j++)             //Change bounds according to camera info instead of isPointInView
            {
                for (int i = 0; i < MAX_TILES; i++)
                {
                    if (_camera.isPointInView(i * TERRAIN_TILE_SIZE, j * TERRAIN_TILE_SIZE))                    //Costly
                    {
                        _sprites[_tiles[j, i].SpriteId].Draw(TERRAIN_TILE_SIZE * i - (int)_camera.Position.X,
                                                             TERRAIN_TILE_SIZE * j - (int)_camera.Position.Y);  //Int cast may be costly at this frequency
                    }
                }
            }
        }
        public void Init()
        {
            //Init map
            _tiles = new Tile[MAX_TILES, MAX_TILES];
            _sprites = new List<Sprite>(10);
        }
        public void Load()
        {
            Console.WriteLine("TerrainManager - Load - needs further implementation");
            //Load sprites
            //txt file with img names and count

            _sprites.Add(new Sprite(new Bitmap("img\\dirt.png")));
            _sprites.Add(new Sprite(new Bitmap("img\\water.png")));
            _sprites.Add(new Sprite(new Bitmap("img\\stone.png")));
            _sprites.Add(new Sprite(new Bitmap("img\\sand.png")));

        }
        public void Update(double dt)
        {

        }
        public void Stop()
        {
            _running = false;
        }
        public bool isRunning()
        {
            return _running;
        }
        string CalculatePath(Point2D startPos, Point2D endPos, int[,] height)
        {
            Console.WriteLine("Calculate Path not implemented");
            return "0123";
        }
        string CalculatePath(Point2D startPos, Point2D endPos)
        {
            Console.WriteLine("Calculte Path not implemented");
            return "0123";
        }
        int[,] getHeightMap()   //return [,] or List<List<int>>
        {
            Console.WriteLine("getHeightMap - not finalised");
            //No idea if this will work XD.
            int[,] h = new int[MAX_TILES,MAX_TILES];
            for (int j = 0; j < MAX_TILES; j++)
            {
                for (int i = 0; i < MAX_TILES; i++)
                {
                    h[j, i] = _tiles[j,i].Height;
                }
            }
            return h;
        }
        List<Point2D> getPoints(string path, Point2D startPos)
        {
            Console.WriteLine("getPoints not implemented correctly");
            int size = path.Length;
			List<Point2D> points = new List<Point2D>(size);

			Point2D currentPos = new Point2D();
            currentPos.X = startPos.X;
            currentPos.Y = startPos.Y;

			for (int i= 0; i < size; i++)
			{
				char c_dir = path[i];
				int dir = c_dir-48;
				currentPos.X += 0;//ns_path_finder::dx[dir];
				currentPos.Y += 0;//ns_path_finder::dy[dir];
				Point2D tempPos = new Point2D();
                tempPos.X = currentPos.X;
                tempPos.Y = currentPos.Y;
				points.Add(tempPos);
			}
			return points;
        }
    }
}
